package com.lwlk.zdk.controller.User;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.annotation.MyLog;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.T.TDisPlatformMapper;
import com.lwlk.zdk.mapper.T.TRegionMapper;
import com.lwlk.zdk.mapper.pw.pwPlatformRoleMapper;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.CommonUser;
import com.lwlk.zdk.model.pw.pwUser;
import com.lwlk.zdk.model.task.pwTaskPlatformAssessmentStatistics;
import com.lwlk.zdk.util.JwtTokenUtil;
import com.lwlk.zdk.util.LayuiTablePagingResult;
import com.lwlk.zdk.util.Response;
import com.lwlk.zdk.util.listerUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/User")
public class UserController {

    @Autowired
    pwUserMapper pwUserMapper;
    @Autowired
    pwPlatformRoleMapper platformRoleMapper;
    @Autowired
    TDisPlatformMapper disPlatformMapper;
    @Autowired
    pwUserMapper userMapper;
    @Autowired
    TRegionMapper tRegionMapper;

    @ApiOperation(value = "添加用户")
    @MyLog(value = "添加用户")  //这里添加了AOP的自定义注解
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Response getAdd(@RequestBody pwUser pwUser,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        Boolean flage=listerUtil.getUser(request,pwUser);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        if (flage==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("不可修改账号所属地区以外的信息");
            return response;
        }

        List<Map<String,Object>> platformList=userMapper.selectOne(pwUser.getUserName());
        if (platformList.size()>0){
            response.setCode(RetCode.ACCOUNT_EXIST);
            return response;
        }
        //pwUser.setPassword(MD5Utils.encrypt(pwUser.getUserName(),pwUser.getPassword()));
        int i=pwUserMapper.insertSelective(pwUser);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "修改用户管理模块中的用户信息")
    @MyLog(value = "修改用户信息")  //这里添加了AOP的自定义注解
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Response getUpdate(@RequestBody pwUser pwUser,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        int i=pwUserMapper.update(pwUser);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "修改个人管理模块中的用户信息")
    @MyLog(value = "修改用户信息")  //这里添加了AOP的自定义注解
    @RequestMapping(value = "/updateByUser",method = RequestMethod.POST)
    public Response geTUpdateByUser(@RequestBody pwUser pwUser,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        int i=pwUserMapper.update(pwUser);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "删除用户信息")
    @MyLog(value = "删除用户信息")  //这里添加了AOP的自定义注解
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Response getDelete(Integer id,Integer level,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        int i=pwUserMapper.deleteByExample(id);
        response.setData(i);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
        }

       @ApiOperation(value = "展示用户信息")
       @RequestMapping(value = "/showUser",method = RequestMethod.GET)
       public LayuiTablePagingResult getShowUser(HttpServletRequest request, Integer page, Integer size, String userName, String name, String mobile, Integer isForbidden, Integer type){
         LayuiTablePagingResult result=null;
         try {
             PageHelper.startPage(page,size);
             String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
             String newToken=authHeader.replaceAll("Bearer ","");
             String accountNumber=JwtTokenUtil.getUsername(newToken,"MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=");
             List<Map<String,Object>>userMessageList=pwUserMapper.selectOne(accountNumber);
             Integer  areaCode=Integer.parseInt(String.valueOf(userMessageList.get(0).get("regionCode")));
             Integer  level=Integer.parseInt(String.valueOf(userMessageList.get(0).get("level")));
                 if (level==1){
                     areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,2));
                 }else if (level==2){
                     areaCode= Integer.valueOf(String.valueOf(areaCode).substring(0,4));
                 }else if (level==3){
                     areaCode= areaCode;
                 }
             List<Map<String,Object>> userList=pwUserMapper.selectFuzzy(userName,name,mobile,isForbidden,type,areaCode);
             for (int i = 0; i < userList.size(); i++) {
                 Map map=userList.get(i);
                 Integer regionCode=null;
                 if (String.valueOf(map.get("regionCode")).equals("null")){
                      regionCode=0;
                 }else {
                     regionCode= Integer.valueOf(String.valueOf(map.get("regionCode")));
                 }
                 String regionCodeName=tRegionMapper.selectAreaCode(regionCode);
                 map.put("regionCodeName",regionCodeName);
             }
             PageInfo<pwTaskPlatformAssessmentStatistics> pageInfo=new PageInfo(userList);
             result = new LayuiTablePagingResult();
             result.setCode(RetCode.SUCCESS.getCode());
             result.setMsg(RetCode.SUCCESS.getMsg());
             result.setCount(pageInfo.getTotal());
             result.setData(pageInfo.getList());
             Long totalPages = pageInfo.getTotal() / size;
             if (pageInfo.getTotal() % size != 0){
                 totalPages ++;
             }
             result.setTotalPages(totalPages);
         }catch (Exception e){
             e.printStackTrace();
         }
         return result;
       }

       @ApiOperation(value = "重置密码")
       @RequestMapping(value = "/resetPassword",method = RequestMethod.POST)
       public Response getResetPassword(String userName){
           Response response=new Response();
           String pwd= "e10adc3949ba59abbe56e057f20f883e";
           int i =pwUserMapper.updatePassword(pwd,userName);
           response.setCode(RetCode.SUCCESS);
           response.setMsg("修改成功");
           response.setData(i);
           return response;
       }

       @ApiOperation(value = "用户详情")
       @RequestMapping(value = "/userDetails",method = RequestMethod.GET)
       public Response getUserDetails(Integer id){
         Response response=new Response();
         List<Map<String,Object>>userDetails=pwUserMapper.selectDetails(id);
           for (int i = 0; i < userDetails.size(); i++) {
               Map map=userDetails.get(i);
               Integer regionCode= Integer.valueOf(String.valueOf(map.get("regionCode")));
               String regionCodeName=tRegionMapper.selectAreaCode(regionCode);
               map.put("regionCodeName",regionCodeName);
           }
         response.setData(userDetails);
         response.setCode(RetCode.SUCCESS);
         response.setMsg("执行成功");
         return response;
       }

       @ApiOperation(value = "角色信息不分页展示")
       @RequestMapping(value = "/roleMessage",method = RequestMethod.GET)
       public Response getRoleMessage(){
        Response response=new Response();
        List<Map<String,Object>>roleList=platformRoleMapper.selectRole();
        response.setData(roleList);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
       }

       @ApiOperation(value = "平台信息不分页展示")
       @RequestMapping(value = "/platformMessage",method = RequestMethod.GET)
       public Response getPlatformMessage(){
        Response response=new Response();
        List<Map<String,Object>>platformList=disPlatformMapper.selectPlatformMessageList();
        response.setData(platformList);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
       }


}

